package facade;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JList;

import utils.Dish;
import utils.PriceCalculator;

import model.Delivery;

import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Font;

import java.util.Iterator;
import java.util.Vector;
import java.util.ArrayList;
import java.util.HashMap;

import java.text.DecimalFormat;

public class Facture extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private Delivery delivery;
	
	public Facture(Delivery d){
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		delivery = d;
		
		setTitle("Facture");
		setBounds((int)dim.getWidth()/2-200, (int)dim.getHeight()/2-300, 400, 600);
		setContentPane(createContainer());
		setBackground(Color.WHITE);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		setVisible(true);
	}
	
	private JPanel createContainer(){
		JPanel container = new JPanel();
		container.add(createElementsList());
		return container;
	}
	
	private JList createElementsList(){
		Vector<String> factureList = new Vector<String>();
		HashMap<String, String> deliveryInfos = delivery.getDeliveryInfos();
		ArrayList<Dish> deliveryElements = delivery.getDeliveryElements();
		Iterator<Dish> deliveryElementsIt = deliveryElements.iterator();
		
		DecimalFormat pricesFormat = new DecimalFormat("0.00");
		
		String[] infos = {"Civic address", "City", "Postal code", "Province", "Country", "Date", "Time"};
		
		//Affichage
		factureList.add(delivery.getRestaurantName());
		factureList.add("Confirmation number : " + delivery.getDeliveryConfirmNumber());
		factureList.add(" ");
		factureList.add(deliveryInfos.get(infos[0]));
		factureList.add(deliveryInfos.get(infos[1])+", "+deliveryInfos.get(infos[3])+", "+deliveryInfos.get(infos[4]));
		factureList.add(deliveryInfos.get(infos[2]));
		factureList.add(deliveryInfos.get(infos[5])+", "+deliveryInfos.get(infos[6]));
		factureList.add(" ");
		
		while(deliveryElementsIt.hasNext()){
			Dish element1 = deliveryElementsIt.next();
			
			String elementText = element1.getName()+" -- "+pricesFormat.format(element1.getPrice())+" $";
			factureList.add(elementText);
		}
		
		factureList.add(" ");
		
		float price = PriceCalculator.getPrice((Iterator<Dish>)delivery.getDeliveryElements().iterator());
    	float taxes = PriceCalculator.getTaxes(price);
    	float totalPrice = PriceCalculator.getTotalPrice(price);
		
		factureList.add("Price : "+pricesFormat.format(price)+" $");
		factureList.add("Taxes : "+pricesFormat.format(taxes)+" $");
		factureList.add("Total price : "+pricesFormat.format(totalPrice)+" $");
		
		JList finalList = new JList(factureList);
		finalList.setFont(new Font("Arial", Font.BOLD, 13));
		finalList.setBounds(0, 0, 400, 600);
		
		return finalList;
	}
}
